﻿using JNPF.Basics.Models;
using JNPF.Utils;
using System;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 数据备份
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.09.20
    /// </summary>
    public class DbBackupBll
    {
        private DbBackupIService service = new DbBackupService();

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="requestParam">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<DbBackupEntity>> GetList(PageInput requestParam)
        {
            try
            {
                return await service.GetList(requestParam);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<DbBackupEntity> GetInfo(string id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        public async Task<int> Delete(DbBackupEntity entity)
        {
            try
            {
                if (entity != null)
                {
                    //删除文件
                    FileHelper.DeleteFile(ConfigurationKey.DataBackupFilePath + entity.FileName);
                }
                return await service.Delete(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 备份数据
        /// </summary>
        public async Task<int> DbBackup()
        {
            var userInfo = UserProvider.Instance.Get();
            var fileName = CommonHelper.SortCode + ".bak";
            var filePath = ConfigurationKey.DataBackupFilePath + fileName;
            //备份数据
            await service.DbBackup(filePath);
            //备份记录
            DbBackupEntity entity = new DbBackupEntity();
            entity.Id = CommonHelper.Guid;
            entity.FileName = fileName;
            entity.FilePath = "/api/Common/Download?encryption=" + userInfo.Id + "|" + fileName + "|dataBackup";
            entity.FileSize = FileHelper.ToFileSize(FileHelper.GetFileSize(filePath));
            entity.CreatorTime = DateTime.Now;
            entity.CreatorUserId = UserProvider.Instance.Get().UserId;
            return await service.Create(entity);
        }

        public async Task<int> Creat()
        {
            var userInfo = UserProvider.Instance.Get();
            var fileName = CommonHelper.SortCode + ".Txt";
            DbBackupEntity entity = new DbBackupEntity();
            entity.Id = CommonHelper.Guid;
            entity.FileName = fileName;
            entity.FilePath = "/api/Common/Download?encryption=" + userInfo.Id + "|" + fileName + "|dataBackup";
            entity.FileSize = "123";
            entity.CreatorTime = DateTime.Now;
            entity.CreatorUserId = UserProvider.Instance.Get().UserId;
            return await service.Create(entity);
        }
    }
}
